//
//  SmsCloudSearchSearchView.m

#import "SmsCloudSearchSearchView.h"
@interface SmsCloudSearchSearchView()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *smsCloudSearchField;
@end
@implementation SmsCloudSearchSearchView
-(instancetype)init{
    if (self == [super init]) {
        self.layer.cornerRadius = [SmsCloudHead SmsCloudScreewRatio:20];
        self.clipsToBounds = true;
        self.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
        [self smsCloudInitView];
    }
    return self;
}
- (void)smsCloudInitView{
    UIView * smsCloudSearchSearchView = [[UIView alloc] init];
    smsCloudSearchSearchView.backgroundColor = UIColor.clearColor;
    smsCloudSearchSearchView.layer.cornerRadius = [SmsCloudHead SmsCloudScreewRatio:10];
    smsCloudSearchSearchView.layer.borderWidth = [SmsCloudHead SmsCloudScreewRatio:2];
    smsCloudSearchSearchView.layer.borderColor = [SmsCloudHead SmsCloudColorHexString:@"#A4A9BC"].CGColor;
    smsCloudSearchSearchView.clipsToBounds = YES;
    [self addSubview:smsCloudSearchSearchView];
    [smsCloudSearchSearchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:24]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:112]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:29]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:72]));
    }];
    UIImageView * smsCloudHeadImgView = [[UIImageView alloc] init];
    smsCloudHeadImgView.image = [SmsCloudHead SmsCloudImageNamed:@"smsCloudSearchHomeHead"];
    [smsCloudSearchSearchView addSubview:smsCloudHeadImgView];
    [smsCloudHeadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:30]));
        make.centerY.equalTo(self);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:28]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:28]));
    }];
    self.smsCloudSearchField = [[UITextField alloc] init];
    self.smsCloudSearchField.textColor = [SmsCloudHead SmsCloudColorHexString:@"#333333"];
    NSMutableDictionary *attrDic = [NSMutableDictionary dictionary];
    attrDic[NSForegroundColorAttributeName] = [SmsCloudHead SmsCloudColorHexString:@"#333333"];
    NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"搜索关键字" attributes:attrDic];
    self.smsCloudSearchField.attributedPlaceholder = placeholder;
    self.smsCloudSearchField.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:28] weight:UIFontWeightRegular];
    self.smsCloudSearchField.delegate = self;
    self.smsCloudSearchField.returnKeyType = UIReturnKeySearch;
    [smsCloudSearchSearchView addSubview:self.smsCloudSearchField];
    [self.smsCloudSearchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(smsCloudHeadImgView.mas_right).offset([SmsCloudHead SmsCloudScreewRatio:13]);
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:30]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.bottom.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
    UIButton * smsCloudSearchBtn = [[UIButton alloc] init];
    [smsCloudSearchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [smsCloudSearchBtn setTitleColor:[SmsCloudHead SmsCloudColorHexString:@"#333333"] forState:UIControlStateNormal];
    smsCloudSearchBtn.titleLabel.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:32] weight:UIFontWeightRegular];
    [smsCloudSearchBtn addTarget:self action:@selector(smsCloudSearch) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:smsCloudSearchBtn];
    [smsCloudSearchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(smsCloudSearchSearchView.mas_right);
        make.centerY.equalTo(self.smsCloudSearchField);
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:45]));
    }];
}
-(void)setSmsCloudSearchContent:(NSString *)smsCloudSearchContent{
    _smsCloudSearchContent = smsCloudSearchContent;
    self.smsCloudSearchField.text = _smsCloudSearchContent;
}
- (void)smsCloudSearch{
    if (self.SmsCloudSearchBlock) {
        self.SmsCloudSearchBlock(self.smsCloudSearchField.text);
    }
}
#pragma mark -- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.SmsCloudSearchBlock) {
        self.SmsCloudSearchBlock(self.smsCloudSearchField.text);
    }
    return YES;
}
@end
